import { routerRedux } from 'dva/router';
import { stringify } from 'qs';
import {
  saveActivity,
  getActivityList,
  getRepairsList,
  pubnishActivityOnline,
  delActivity,
  editActivity,
  getEnrollList,
  setEnroll2Contacted
} from '@/services/http';

export default {
  namespace: 'activity',
  state: {
    activityList: [],
    total: 0,
    isLoading: true,
    ret: {},

    enrollList: [],
    enrollLoading: true,
    enrollTotal: 0,
  },

  effects: {
    /**
     * 获取活动列表
     * @param payload
     * @param call
     * @param put
     * @returns {IterableIterator<*>}
     */
    *getActivityList(payload, { call, put }) {
      const response = yield call(getActivityList, payload.payload);
      yield put({
        type: '_getActivityList',
        data: response,
      });
    },

    /**
     * 发布新活动
     * @param payload
     * @param call
     * @param put
     * @returns {IterableIterator<*>}
     */
    *saveActivity(payload, { call, put }) {
      const { saveActivityCallback } = payload.payload;
      const response = yield call(saveActivity, payload.payload);
      saveActivityCallback(response);
    },

    /**
     * 编辑活动
     * @param payload
     * @param call
     * @param put
     * @returns {IterableIterator<*>}
     */
    *editActivity(payload, { call, put }) {
      const { onEditActivityCallback } = payload.payload;
      const response = yield call(editActivity, payload.payload);
      onEditActivityCallback(response);
    },

    /**
     * 修改活动状态，同时只能有一个活动在线
     * @param payload
     * @param call
     * @param put
     * @returns {IterableIterator<*>}
     */
    *pubnishActivityOnline(payload, { call, put }) {
      const response1 = yield call(pubnishActivityOnline, payload.payload);
      if (response1.code == '1') {
        const response2 = yield call(getActivityList, payload.payload);
        yield put({
          type: '_getActivityList',
          data: response2,
        });
      } else {
        yield put({
          type: '_getActivityList',
          data: { code: 0, ret: [] },
        });
      }
    },

    /**
     *
     * @param payload
     * @param call
     * @param put
     * @returns {IterableIterator<*>}
     */
    *delActivity(payload, { call, put }) {
      const response1 = yield call(delActivity, payload.payload);
      const response2 = yield call(getActivityList, payload.payload);
      yield put({
        type: '_getActivityList',
        data: response2,
      });
    },

    /**
     * 获取活动联系人
     * @param payload
     * @param call
     * @param put
     * @returns {IterableIterator<*>}
     */
    *getEnrollList(payload, { call, put }) {
      yield put({
        type: 'updateEnrollLoading',
        enrollLoading: true,
      })
      const response = yield call(getEnrollList, payload.payload);
      yield put({
        type: '_getEnrollList',
        data: response,
      });
    },
    /**
     * 联系过
     * @param payload
     * @param call
     * @param put
     * @returns {IterableIterator<*>}
     */
    *setEnroll2Contacted(payload, { call, put }) {
      // const { onCallBack } = payload.payload;
      const response = yield call(setEnroll2Contacted, payload.payload);
      // onCallBack(response);
    },
  },

  reducers: {
    _getActivityList(state, action) {
      return {
        ...state,
        activityList: action.data.code == 1 ? action.data.ret : [],
        total: action.data.code == 1 ? action.data.total : 0,
        isLoading: false,
      };
    },
    updateEnrollLoading(state, action) {
      return {
        ...state,
        enrollLoading: action.enrollLoading
      }
    },
    _getEnrollList(state, action) {
      return {
        ...state,
        enrollLoading: false,
        enrollList: action.data.code == 1 ? action.data.ret : [],
        enrollTotal: action.data.code == 1 ? action.data.total : 0,
      };
    },
  },
};
